package com.myBlog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.myBlog.domain.entity.User;
import com.myBlog.domain.vo.CommentVo;
import com.myBlog.constants.SystemConstants;
import com.myBlog.domain.ResponseResult;
import com.myBlog.domain.entity.Comment;
import com.myBlog.domain.vo.PageVo;
import com.myBlog.enums.AppHttpCodeEnum;
import com.myBlog.mapper.CommentMapper;
import com.myBlog.mapper.UserMapper;
import com.myBlog.service.CommentService;
import com.myBlog.utils.BeanCopyUtils;
import com.myBlog.exception.SystemException;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;


/**
 * 评论表(SgComment)表服务实现类
 *
 * @author makejava
 * @since 2023-03-15 15:22:45
 */
@Service
public class CommentServiceImpl extends ServiceImpl<CommentMapper, Comment> implements CommentService {
    @Resource
    private UserMapper userMapper;

    /**
     * 条件分页查询评论
     * @param articleId
     * @param pageNum
     * @param pageSize
     * @return
     */
    @Override
    public ResponseResult commentList(String type, Long articleId, Integer pageNum, Integer pageSize) {
        //查询对应文章的根评论
        //articleId
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SystemConstants.ARTICLE_COMMENT.equals(type), Comment::getArticleId, articleId);
        //根评论rootId -1
        wrapper.eq(Comment::getRootId, SystemConstants.ROOT_ID);
        wrapper.eq(Comment::getType, type);
        //分页查询
        Page<Comment> page = new Page<>(pageNum, pageSize);
        this.page(page, wrapper);
        List<Comment> pageRecords = page.getRecords();
        List<CommentVo> commentVos = getCommentVoList(pageRecords);
        //查询所有根评论对应的子评论集合，并且赋值给对应的属性
        commentVos = commentVos.stream()
                .peek(commentVo -> commentVo
                        .setChildren(getChildrenList(commentVo.getId())))
                .collect(Collectors.toList());
        return ResponseResult.okResult(new PageVo(commentVos, page.getTotal()));
    }
    /**
     * 根据根评论的id查询所对应子评论的集合
     *
     * @param id
     * @return
     */
    private List<CommentVo> getChildrenList(Long id) {
        LambdaQueryWrapper<Comment> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Comment::getRootId, id);
        wrapper.orderByAsc(Comment::getCreateTime);
        List<Comment> commentList = list(wrapper);
        List<CommentVo> commentVoList = getCommentVoList(commentList);
        return commentVoList;
    }
    /**
     * 添加评论
     * @param
     * @return
     */
    @Override
    public ResponseResult addComment(Comment comment) {
        if (!StringUtils.hasText(comment.getContent())) {
            throw new SystemException(AppHttpCodeEnum.COMMENT_NOT_NULL);
        }
        save(comment);
        return ResponseResult.okResult();
    }



    private List<CommentVo> getCommentVoList(List<Comment> list) {
        List<CommentVo> commentVos = BeanCopyUtils.copyBeanList(list, CommentVo.class);

        for (CommentVo commentVo : commentVos) {
            //遍历Vo集合通过createBy查询用户昵称并赋值
            User user = userMapper.selectById(commentVo.getCreateBy());
            commentVo.setUserName(user.getNickName());
            //通过toCommentUserId查询用户的昵称并赋值
            //如果toCommentUserId不为-1
            if (commentVo.getToCommentUserId() != -1) {
                String toCommentUserName = userMapper.selectById(commentVo.getToCommentUserId()).getNickName();
                commentVo.setToCommentUserName(toCommentUserName);
            }

        }
        return commentVos;
    }
}

